package com.chenyuxin.ilp.service.user.task;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.chenyuxin.ilp.mapper.user.UserStatisticsMapper;
import com.chenyuxin.ilp.model.entity.user.UserStatistics;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Component
public class RedisToMySQLSyncTask {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private UserStatisticsMapper userStatisticsMapper;
    // 每 5 分钟同步一次
    @Scheduled(cron = "0 0/5 * * * ?")
    public void syncViewToDB() {
        // Redis key 前缀
        String viewPrefix = "open:view:total:";
        // 获取所有 view 相关的键
        Set<String> keys = stringRedisTemplate.keys(viewPrefix + "*");
        if (keys.isEmpty()) {
            return; // 无数据需要同步
        }
        for (String key : keys) {
            String userId = key.substring(key.lastIndexOf(":") + 1);
            String num = stringRedisTemplate.opsForValue().get(key);
            LambdaUpdateWrapper<UserStatistics> wrapper = new LambdaUpdateWrapper<>();
            wrapper.eq(UserStatistics::getId,userId)
                    .setIncrBy(UserStatistics::getOpenFileView,Integer.parseInt(num));
            userStatisticsMapper.update(wrapper);
            stringRedisTemplate.delete(key);
        }
    }
    @Scheduled(cron = "0 0/5 * * * ?")
    public void syncPreviewToDB() {
        // Redis key 前缀
        String previewPrefix = "open:preview:total:";
        // 获取所有 view 相关的键
        Set<String> keys = stringRedisTemplate.keys(previewPrefix + "*");
        if (keys.isEmpty()) {
            return; // 无数据需要同步
        }
        for (String key : keys) {
            String userId = key.substring(key.lastIndexOf(":") + 1);
            String num = stringRedisTemplate.opsForValue().get(key);
            LambdaUpdateWrapper<UserStatistics> wrapper = new LambdaUpdateWrapper<>();
            wrapper.eq(UserStatistics::getId,userId)
                    .setIncrBy(UserStatistics::getOpenFilePreview,Integer.parseInt(num));
            userStatisticsMapper.update(wrapper);
            stringRedisTemplate.delete(key);
        }
    }
    @Scheduled(cron = "0 0/5 * * * ?")
    public void syncDownloadToDB() {
        // Redis key 前缀
        String downloadPrefix = "open:download:total:";
        // 获取所有 view 相关的键
        Set<String> keys = stringRedisTemplate.keys(downloadPrefix + "*");
        if (keys.isEmpty()) {
            return; // 无数据需要同步
        }
        for (String key : keys) {
            String userId = key.substring(key.lastIndexOf(":") + 1);
            String num = stringRedisTemplate.opsForValue().get(key);
            LambdaUpdateWrapper<UserStatistics> wrapper = new LambdaUpdateWrapper<>();
            wrapper.eq(UserStatistics::getId,userId)
                    .setIncrBy(UserStatistics::getOpenFileDownload,Integer.parseInt(num));
            userStatisticsMapper.update(wrapper);
            stringRedisTemplate.delete(key);
        }
    }

}
